
#include "dios_impl.h"
#include "kernels/dios_ssp_api.h"

#include <pthread.h>

// #define BF
#define AECBF

typedef struct diosHandle_
{
    objSSP_Param *cfg;
    void *handle;
}diosHandle;

void *dios_create()
{
    diosHandle *self = (diosHandle *)malloc(sizeof(diosHandle));
    self->cfg = (objSSP_Param *)malloc(sizeof(objSSP_Param));
    self->cfg->AEC_KEY = 1;
    self->cfg->NS_KEY = 1;
    self->cfg->AGC_KEY = 0;
    self->cfg->BF_KEY = 0;
    self->cfg->DOA_KEY = 0;
    self->cfg->HPF_KEY = 1;
    self->cfg->mic_num = 1;
    self->cfg->ref_num = 1;
    self->cfg->loc_phi = 90.0f;    
    self->handle = dios_ssp_init_api(self->cfg);
    dios_ssp_reset_api(self->handle, self->cfg);    
    return (void *)self;
}

void *dios_create_mics(int mics, float (*pos)[3])
{
    diosHandle *self = (diosHandle *)malloc(sizeof(diosHandle));
    self->cfg = (objSSP_Param *)malloc(sizeof(objSSP_Param));
    #ifdef BF
    self->cfg->AEC_KEY = 0;
    self->cfg->NS_KEY = 0;
    self->cfg->AGC_KEY = 0;
    self->cfg->BF_KEY = 2;
    self->cfg->DOA_KEY = 1;
    self->cfg->HPF_KEY = 0;
    self->cfg->mic_num = mics;
    self->cfg->ref_num = 1;
    self->cfg->loc_phi = 90.0f;   
    /**/
    for (int m=0; m<mics; m++) {
        self->cfg->mic_coord[m].x = pos[m][0];
        self->cfg->mic_coord[m].y = pos[m][1];
        self->cfg->mic_coord[m].z = pos[m][2];
        printf("pos = %d, [%.3f, %.3f, %.3f]\n", m, self->cfg->mic_coord[m].x, self->cfg->mic_coord[m].y, self->cfg->mic_coord[m].z);
    }
    #endif
    #ifdef AECBF
    self->cfg->AEC_KEY = 1;
    self->cfg->NS_KEY = 0;
    self->cfg->AGC_KEY = 0;
    self->cfg->BF_KEY = 2;
    self->cfg->DOA_KEY = 1;
    self->cfg->HPF_KEY = 1;
    self->cfg->mic_num = mics;
    self->cfg->ref_num = 1;
    self->cfg->loc_phi = 90.0f;   
    /**/
    for (int m=0; m<mics; m++) {
        self->cfg->mic_coord[m].x = pos[m][0];
        self->cfg->mic_coord[m].y = pos[m][1];
        self->cfg->mic_coord[m].z = pos[m][2];
        printf("pos = %d, [%.3f, %.3f, %.3f]\n", m, self->cfg->mic_coord[m].x, self->cfg->mic_coord[m].y, self->cfg->mic_coord[m].z);
    }
    #endif
        
    self->handle = dios_ssp_init_api(self->cfg);
    dios_ssp_reset_api(self->handle, self->cfg);    
    return (void *)self;
}


int dios_process_frame(void *obj, short *mic, short *ref, short *aec)
{
    diosHandle *self = (diosHandle *)obj;    
    dios_ssp_process_api(self->handle, mic, ref, aec, self->cfg);    
    return 0;
}

void dios_destroy(void *obj)
{
    diosHandle *self = (diosHandle *)obj;
    dios_ssp_uninit_api(self->handle, self->cfg);
    free(self);
}
